<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Darabonba\GatewaySpi\Models\InterceptorContext\response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Coupon extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return  view("admin.coupon.index");
    }

    public function getCoupons()
    {
       $list = DB::table('coupon')->orderByDesc('id')->get();
       $total = DB::table('coupon')->count();
        return response()->json(['code'=>0,'data'=>$list,'count'=>$total]);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.coupon.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $params = $request->except(['_token','s']);
       $params['is_on'] = $params['is_on']=='on'?1:0;
       $from_to_date = explode(" - ",$params['from_to_date']);
       $params['from_time'] = strtotime($from_to_date[0]);
       $params['to_time'] = strtotime($from_to_date[1]);
       unset($params['from_to_date']);
       if(DB::table('coupon')->insertGetId($params)) {
           return \response()->json(['status'=>200,'msg'=>'添加成功']);
       }else{
           return \response()->json(['status'=>201,'msg'=>'添加失败']);
       }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
